//
//  FeautreController.swift
//  WeiBo001
//
//  Created by ZhiQiang Li on 16/7/24.
//  Copyright © 2016年 ZhiQiang Li. All rights reserved.
//

import UIKit
private let featureID = "cell"

class FeautreController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
     setupUI()

}
 //设置视图
   func setupUI(){
    view.addSubview(collectionView)
    // 添加约束
    collectionView.snp_makeConstraints { (make) -> Void in
        make.edges.equalTo(view).offset(UIEdgeInsetsZero)
        
    }
   
    
    }
    
    //懒加载控件
    private lazy var collectionView:UICollectionView  = {
        // 创建一个layout
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = CGSize(width: SCREENW, height: SCREENH)
        // 设置垂直和水平间距
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        //设置滚动方向
        layout.scrollDirection = .Horizontal
        let view = UICollectionView(frame: CGRectZero, collectionViewLayout: layout)
        //注册
        view.registerClass(featureViewCell.self, forCellWithReuseIdentifier:featureID)
        view.backgroundColor = RandomColor()
        view.dataSource = self
        view.delegate = self
        // 设置分页
        view.pagingEnabled = true
        // 取消弹簧效果
        view.bounces = false
        // 隐藏滚动条
        view.showsHorizontalScrollIndicator = false
        view.showsVerticalScrollIndicator = false
        
       return view
    }()
    
    
}

extension FeautreController:UICollectionViewDataSource,UICollectionViewDelegate {
    
    
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 4
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        // 创建一个cell
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(featureID, forIndexPath: indexPath) as! featureViewCell
        cell.imageIndex = indexPath.item
        return cell
    }
    func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        let index = Int(scrollView.contentOffset.x / SCREENW)
        // 代表是最后一个cell
        if index == 3 {
            let cell = self.collectionView.cellForItemAtIndexPath(NSIndexPath(forItem: index, inSection: 0)) as! featureViewCell
            // 开始动画
            cell.startAnim()
        }
    }
}



//自定义Cell
class featureViewCell:UICollectionViewCell {
    var imageIndex: Int = 0 {
        didSet{
            bgImageView.image = UIImage(named: "new_feature_\(imageIndex + 1)")
            startButton.hidden = true
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func startAnim(){
        startButton.hidden = false
        self.startButton.transform = CGAffineTransformMakeScale(0, 0)
        self.startButton.userInteractionEnabled = false
        
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: { () -> Void in
            self.startButton.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
                self.startButton.userInteractionEnabled = true
        }
    }
//监听方法
    func buttonClick(){
        // 发送通知 告知appdelegate 切换根控制器
        NSNotificationCenter.defaultCenter().postNotificationName(WBSwitchRootViewControllerNoti, object: "newFeature")
    }


    
    // : - 设置视图
    private func setupUI(){
        backgroundColor = RandomColor()
        // 添加控件
        contentView.addSubview(bgImageView)
        contentView.addSubview(startButton)
        // 添加约束
        bgImageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }
        
        startButton.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(contentView)
            make.bottom.equalTo(contentView).offset(-100)
        }
        
        // 默认开始按钮全部隐藏
        startButton.hidden = true
        
    }
    
    //  - 懒加载控件
    // 背景图片
    private lazy var bgImageView: UIImageView = UIImageView()
    // 开始按钮
    private lazy var startButton: UIButton = {
        let button = UIButton()
        // 添加点击事件
        button.addTarget(self, action: "buttonClick", forControlEvents: UIControlEvents.TouchUpInside)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        // 设置title
        button.setTitle("欢迎光临", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(18)
        button.sizeToFit()
        return button
    }()

    
}

